package com.by.controller;

import com.by.constant.MessageConstant;
import com.by.entity.PageResult;
import com.by.entity.QueryPageBean;
import com.by.entity.Result;
import com.by.pojo.CheckGroup;
import com.by.pojo.Setmeal;
import com.by.service.CheckGroupService;
import com.by.service.SetmealService;
import com.by.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    /**
    * @Description: 新增套餐
            * @Param: * @param checkgroupIds
     * @param setmeal
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/11
            */
    @RequestMapping("/add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        try {
            setmealService.add(checkgroupIds, setmeal);
            //把图片保存到setmeal_pic_db中
            setmealService.addSetmealPicDb(setmeal.getImg());
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {
            //1、图片重命名（yyy.xxx.jpg）
            String originalFilename = imgFile.getOriginalFilename();
            //后缀名
            String endName = originalFilename.substring(originalFilename.lastIndexOf('.'));
            //随机生成的名字
            String beginName = UUID.randomUUID().toString();
            //修改后的图片名称：前缀+后缀
            String imgName = beginName + endName;
            System.out.println("-----------------imgName:"+imgName);

            //2、上传图片
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),imgName);
            //把图片保存到setmeal_pic中
            setmealService.addSetmealPic(imgName);

            return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,imgName);
        } catch (IOException e) {
            e.printStackTrace();
            return  new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    /**
    * @Description: 分页查询
            * @Param: * @param queryPageBean
            * @return: com.by.entity.PageResult
            * @Author: GP
            * @Date: 2024/1/12
            */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    /**
    * @Description: 根据套餐id来查询套餐信息
            * @Param: * @param id
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/12
            */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
    * @Description: 根据套餐ID来查询检查组的ID，传递给前端的编辑选项卡中的检查组Tab栏，进行回显。
            * @Param: * @param id
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/12
            */
    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){
        try {
            List<Integer> checkGroupList = setmealService.findCheckGroupIdsBySetmealId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
    * @Description: 编辑套餐
            * @Param: * @param checkgroupIds
     * @param setmeal
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/12
            */
    @RequestMapping("/edit")
    public Result eidt(Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        try {
            setmealService.edit(checkgroupIds, setmeal);
            //把图片保存到setmeal_pic_db中
            setmealService.addSetmealPicDb(setmeal.getImg());
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    /**
    * @Description: 根据套餐id删除套餐
            * @Param: * @param id
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/13
            */
    @RequestMapping("/delete")
    public Result deleteSetmealById(Integer id,String img){
        try {
            setmealService.deleteSetmealById(id,img);
            System.out.println("delere"+img);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
//
//    public static void main(String[] args) {
//        String pic = "yyy.xxx.jpg";
//
//        //太死板
//        //String endName = pic.substring(3);
//
//        //第一个.的位置，0开始
//        System.out.println(pic.indexOf("."));
//        //最后一个.的位置，0开始
//        System.out.println(pic.lastIndexOf("."));
//
//        //从最后一个.的位置开始截取
//        String endName = pic.substring(pic.lastIndexOf("."));
//        System.out.println(endName);

//        for (int i = 0; i < 50; i++) {
//            //随机生成的字符串 且是全球唯一的
//            String uuid = UUID.randomUUID().toString();
//            System.out.println(uuid);
//        }
//    }
}
